﻿@page "/UserIndex"
@using Carlife.Inf;
@using CarlifeService;
@using Commonality.DTO.AuthorityDTO;
@using ICarlifeService;
@using Microsoft.EntityFrameworkCore;
@using Models.Authority;
@using ICarlifeRespository;
@using CarlifeRespository;
@inject IUsersService basek;
@inject NavigationManager NavigationManager
@inject IJSRuntime jsRuntime
<PageTitle>用户列表</PageTitle>
<h3>用户列表</h3>
<EditForm Model="@there">
    <div class="input-group mb-3">
        <div class="accordion-body col-1 col-sm-1">
            <span class="input-group-text" id="inputGroup-sizing-default">用户名:</span>
        </div>
        <div class="accordion-body col-12 col-sm-2">
            <InputText @bind-Value="UserName" class="form-control" aria-describedby="inputGroup-sizing-default" PlaceHolder="请输入用户名"></InputText>
        </div>
        <div class="accordion-body col-1 col-sm-1">
            <span class="input-group-text" id="inputGroup-sizing-default">真实姓名:</span>
        </div>
        <div class="accordion-body col-12 col-sm-2">
            <InputText @bind-Value="RealName" class="form-control" aria-describedby="inputGroup-sizing-default" PlaceHolder="请输入真实姓名"></InputText>
        </div>
        <button id="search" class="btn btn-outline-secondary" @onclick="SelectShou">查询</button>
    </div>
</EditForm>
<table class="table-bordered accountTable" width="90%">
    <thead>
        <tr>
            <th>登录帐号</th>
            <th>用户名称</th>
            <th>真实姓名</th>
            <th>英文名称</th>
            <th>性别</th>
            <th>国际电话</th>
            <th>移动电话</th>
            <th>邮箱</th>
            <th>状态</th>
            <th>公司名称</th>
            <th>启用禁用</th>
            <th>操作</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in there)
        {
            <tr>
                <td>@item.Login_Name</td>
                <td>@item.UserName</td>
                <td>@item.RealName</td>
                <td>@item.EnglishName</td>
                @if (@item.Gender == true)
                {
                    <td>男</td>
                }
                else
                {
                    <td>女</td>
                }
                <td>@item.Phone</td>
                <td>@item.Mobile</td>
                <td>@item.Email</td>
                <td>@item.Type</td>
                <td>@item.DropName</td>
                @if (@item.Enabled == true)
                {
                    <td>启用</td>
                }
                else
                {
                    <td>禁用</td>
                }
                <td>
                    @if (@item.Enabled == true)
                    {
                        <a href="/UserUpdate/@item.UsleID">编辑</a>
                        <button @onclick="()=>RemoveUsers(item.UsleID)">删除</button>
                        <a href="/UserUpdate">重置密码</a>
                    }
                    else
                    {
                        <button @onclick="()=>RemoveUsers(item.UsleID)">删除</button>
                    }
                </td>
            </tr>
        }
    </tbody>
@*    <Table TItem="UserInfoDTO"
           IsPagination="true" PageItemsSource="@PageItemsSource" OnQueryAsync="@OnQueryAsync" ShowAddButton="true">
        <TableColumns>
            <TableColumn Text="Id" @bind-Field="@context.UsleID" Width="180" />
        </TableColumns>
    </Table>*@
</table>
@code {
    private List<UserInfoDTO> there = new List<UserInfoDTO>();
    public string? UserName { get; set; }
    public string? RealName { get; set; }
    protected override async Task OnInitializedAsync()
    {
         there = basek.GetUserAll();
        await base.OnInitializedAsync();
    }
    private void AddUser()
    {
        NavigationManager.NavigateTo($"UserInfoAdd");
    }
    private async void RemoveUsers(int id)
    {
        bool result = await jsRuntime.InvokeAsync<bool>("confirm", "确定要删除吗?");
        if (result)
        {
           int rou= basek.GetUserRemove(id);
            if (rou > 0)
            {
                await jsRuntime.InvokeAsync<string>("alert", "删除成功 !");
                OnInitializedAsync();
                StateHasChanged();
            }
            else
            {
                await jsRuntime.InvokeAsync<string>("alert", "删除失败 !");
                return;
            }
        }
    }
    //查询
    private void SelectShou()
    {
        there = basek.GetUserAll(UserName, RealName);
    }
    private List<UserInfoDTO>? Items { get; set; }
    private static IEnumerable<int> PageItemsSource => new int[]
    {
        1,
        2,
        3,
    };
    private Task<QueryData<UserInfoDTO>> OnQueryAsync(QueryPageOptions options)
    {
        IEnumerable<UserInfoDTO> items = Items;
        var total = items.Count();
        items = items.Skip((options.PageIndex - 1) * options.PageItems).Take(options.PageItems).ToList();
        return Task.FromResult(new QueryData<UserInfoDTO>() { Items = items, TotalCount = total, IsSorted = true, IsFiltered = true, IsSearch = true });
    }
}